// 持仓管理 Mock 数据
export interface Position {
  id: number
  symbol: string
  coinName: string
  coinIcon: string
  side: 'long' | 'short'
  size: number
  entryPrice: number
  currentPrice: number
  pnl: number
  pnlPercent: number
  margin: number
  leverage: number
  liquidationPrice: number
  unrealizedPnl: number
  realizedPnl: number
  openTime: string
  strategyName: string
  exchangeName: string
  status: 'active' | 'closing' | 'closed'
  stopLoss?: number
  takeProfit?: number
  trailingStop?: boolean
  autoClose?: boolean
}

export interface PositionSummary {
  totalPositions: number
  activePositions: number
  totalMargin: number
  totalUnrealizedPnl: number
  totalRealizedPnl: number
  totalPnl: number
  marginRatio: number
  riskLevel: 'low' | 'medium' | 'high'
}

// Mock 持仓数据
export const mockPositions: Position[] = [
  {
    id: 1,
    symbol: 'BTCUSDT',
    coinName: 'Bitcoin',
    coinIcon: '₿',
    side: 'long',
    size: 0.5,
    entryPrice: 43250.00,
    currentPrice: 44180.50,
    pnl: 465.25,
    pnlPercent: 2.15,
    margin: 2162.50,
    leverage: 10,
    liquidationPrice: 38925.00,
    unrealizedPnl: 465.25,
    realizedPnl: 0,
    openTime: '2024-02-20 14:30:25',
    strategyName: 'BTC蜻蜓Dragonfly策略',
    exchangeName: 'Binance',
    status: 'active',
    stopLoss: 42000.00,
    takeProfit: 46000.00,
    trailingStop: true,
    autoClose: false
  },
  {
    id: 2,
    symbol: 'ETHUSDT',
    coinName: 'Ethereum',
    coinIcon: 'Ξ',
    side: 'long',
    size: 8.2,
    entryPrice: 2580.30,
    currentPrice: 2642.85,
    pnl: 512.91,
    pnlPercent: 2.42,
    margin: 2115.85,
    leverage: 10,
    liquidationPrice: 2322.27,
    unrealizedPnl: 512.91,
    realizedPnl: 0,
    openTime: '2024-02-20 16:45:12',
    strategyName: 'ETH MACD黄金交叉策略',
    exchangeName: 'Binance',
    status: 'active',
    stopLoss: 2450.00,
    takeProfit: 2800.00,
    trailingStop: false,
    autoClose: true
  },
  {
    id: 3,
    symbol: 'SOLUSDT',
    coinName: 'Solana',
    coinIcon: '◎',
    side: 'short',
    size: 25.0,
    entryPrice: 98.45,
    currentPrice: 96.22,
    pnl: 55.75,
    pnlPercent: 2.26,
    margin: 491.13,
    leverage: 5,
    liquidationPrice: 108.30,
    unrealizedPnl: 55.75,
    realizedPnl: 0,
    openTime: '2024-02-20 18:20:08',
    strategyName: 'SOL网格交易策略',
    exchangeName: 'OKX',
    status: 'active',
    stopLoss: 102.00,
    takeProfit: 92.00,
    trailingStop: false,
    autoClose: false
  },
  {
    id: 4,
    symbol: 'ADAUSDT',
    coinName: 'Cardano',
    coinIcon: '₳',
    side: 'long',
    size: 850.0,
    entryPrice: 0.4825,
    currentPrice: 0.4698,
    pnl: -107.95,
    pnlPercent: -2.63,
    margin: 820.25,
    leverage: 5,
    liquidationPrice: 0.3860,
    unrealizedPnl: -107.95,
    realizedPnl: 0,
    openTime: '2024-02-19 22:15:33',
    strategyName: 'ADA波动率突破策略',
    exchangeName: 'Binance',
    status: 'active',
    stopLoss: 0.4550,
    takeProfit: 0.5200,
    trailingStop: true,
    autoClose: false
  },
  {
    id: 5,
    symbol: 'DOTUSDT',
    coinName: 'Polkadot',
    coinIcon: '●',
    side: 'long',
    size: 65.0,
    entryPrice: 7.85,
    currentPrice: 8.12,
    pnl: 17.55,
    pnlPercent: 3.44,
    margin: 255.25,
    leverage: 2,
    liquidationPrice: 3.93,
    unrealizedPnl: 17.55,
    realizedPnl: 0,
    openTime: '2024-02-20 10:08:45',
    strategyName: 'DOT均线多空策略',
    exchangeName: 'OKX',
    status: 'active',
    stopLoss: 7.40,
    takeProfit: 8.50,
    trailingStop: false,
    autoClose: true
  },
  {
    id: 6,
    symbol: 'LINKUSDT',
    coinName: 'Chainlink',
    coinIcon: '⛓',
    side: 'short',
    size: 45.0,
    entryPrice: 15.82,
    currentPrice: 15.95,
    pnl: -5.85,
    pnlPercent: -0.82,
    margin: 356.48,
    leverage: 2,
    liquidationPrice: 23.73,
    unrealizedPnl: -5.85,
    realizedPnl: 0,
    openTime: '2024-02-20 20:30:15',
    strategyName: 'LINK RSI反转策略',
    exchangeName: 'Binance',
    status: 'closing',
    stopLoss: 16.50,
    takeProfit: 14.80,
    trailingStop: false,
    autoClose: false
  },
  {
    id: 7,
    symbol: 'MATICUSDT',
    coinName: 'Polygon',
    coinIcon: '⬟',
    side: 'long',
    size: 420.0,
    entryPrice: 0.8950,
    currentPrice: 0.9185,
    pnl: 98.70,
    pnlPercent: 2.62,
    margin: 1883.50,
    leverage: 2,
    liquidationPrice: 0.4475,
    unrealizedPnl: 98.70,
    realizedPnl: 0,
    openTime: '2024-02-20 12:45:22',
    strategyName: 'MATIC网格加仓策略',
    exchangeName: 'OKX',
    status: 'active',
    stopLoss: 0.8500,
    takeProfit: 0.9800,
    trailingStop: true,
    autoClose: true
  },
  {
    id: 8,
    symbol: 'BNBUSDT',
    coinName: 'BNB',
    coinIcon: '🔶',
    side: 'long',
    size: 3.2,
    entryPrice: 315.80,
    currentPrice: 318.45,
    pnl: 8.48,
    pnlPercent: 0.84,
    margin: 1010.56,
    leverage: 1,
    liquidationPrice: 0,
    unrealizedPnl: 8.48,
    realizedPnl: 0,
    openTime: '2024-02-20 09:20:18',
    strategyName: 'BNB套利Alpha策略',
    exchangeName: 'Binance',
    status: 'active',
    stopLoss: 312.00,
    takeProfit: 322.00,
    trailingStop: false,
    autoClose: true
  }
]

// Mock 持仓统计数据
export const mockPositionSummary: PositionSummary = {
  totalPositions: 8,
  activePositions: 7,
  totalMargin: 9095.52,
  totalUnrealizedPnl: 1044.38,
  totalRealizedPnl: 2856.92,
  totalPnl: 3901.30,
  marginRatio: 0.68,
  riskLevel: 'medium'
}

// Mock API 函数
export const fetchPositions = (): Promise<Position[]> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(mockPositions)
    }, 800)
  })
}

export const fetchPositionSummary = (): Promise<PositionSummary> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(mockPositionSummary)
    }, 600)
  })
}

export const closePosition = (positionId: number): Promise<boolean> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      const position = mockPositions.find(p => p.id === positionId)
      if (position) {
        position.status = 'closed'
        resolve(true)
      } else {
        resolve(false)
      }
    }, 1000)
  })
}

export const updateStopLoss = (positionId: number, stopLoss: number): Promise<boolean> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      const position = mockPositions.find(p => p.id === positionId)
      if (position) {
        position.stopLoss = stopLoss
        resolve(true)
      } else {
        resolve(false)
      }
    }, 500)
  })
}

export const updateTakeProfit = (positionId: number, takeProfit: number): Promise<boolean> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      const position = mockPositions.find(p => p.id === positionId)
      if (position) {
        position.takeProfit = takeProfit
        resolve(true)
      } else {
        resolve(false)
      }
    }, 500)
  })
} 